<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('申请标题')" prop="actTitle">
            <el-input v-model="form.actTitle" :placeholder="$t('申请标题')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('申请人')" prop="applicant">
            <UserSelect
              v-model="form.applicant"
              :placeholder="$t('申请人')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('申请部门')" prop="deptId">
            <DeptSelect v-model="form.deptId" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('申请时间')" prop="applicationTime">
            <el-date-picker
              v-model="form.applicationTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('申请时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('申请原因')" prop="applicationReason">
            <el-input v-model="form.applicationReason" :placeholder="$t('申请原因')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('申请类型')" prop="applyType">
            <dict-select
              v-model="form.applyType"
              dictionary="apply_type"
              :placeholder="$t('申请类型')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('申请内容')" prop="applyContent">
            <el-input v-model="form.applyContent" type="textarea" :placeholder="$t('申请内容')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" data-type="account_management" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <FormBtn ref="formBtn" :bus-data="form" has-permi="oa:accountManagement" :menu-key="menuKey" />
    </div>
  </div>
</template>

<script>
    import { saveOaAccountManagement, getOaAccountManagement } from '@/api/oa/daily/accountManagement/oaAccountManagement'
    import { getUser, listUseByDeptId } from '@/api/system/user'
    export default {
        components: {
        },
        data() {
            return {
              // 菜单标识
              menuKey: 'oa_account_management',
                // 表单参数
                form: {
                  fileList: []
                },
                // 表单校验
                rules: {
                    actTitle: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                    deptId: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                    applicant: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                    applicationTime: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                    applicationReason: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                    applyType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                    applyContent: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                },
                // 遮罩层
                loading: false,
                // 是否隐藏其它按钮
                otherBtnHide: false,
                // 根据部门id，当前所选部门下所有用户
                userOption: [],
                // 操作按钮默认不展示
                buttonShow: true
            }
        },
        watch: {
          'form.applicant': {
            handler(val) {
              if (val !== undefined && val !== null && val !== '') { this.userInfo(val) }
            },
            deep: true,
            immediate: true
          }
        },
        created() {
            this.getDicts('apply_type').then(response => {
                this.applyTypeOptions = response.data
            })
            this.$bus.$emit('async_done')
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getOaAccountManagement(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 根据选择的用户不同显示部门
            userInfo(userId) {
              getUser(userId).then(response => {
                this.form.deptId = response.data.deptId
              })
            },
            // 根据部门id查询当前部门下的所有员工
            findUserByDeptId(deptId) {
              listUseByDeptId(deptId).then(response => {
                this.userOption = response.data
              })
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    actTitle: undefined,
                    deptId: undefined,
                    applicant: this.$store.state.user.userId,
                    applicationTime: new Date(),
                    applicationReason: undefined,
                    applyType: undefined,
                    actId: undefined,
                    actStatus: undefined,
                    applyContent: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined,
                    fileList: []
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                    this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm: function(type) {
              return new Promise((resolve, reject) => {
                  this.$refs['form'].validate(valid => {
                    // 验证数据
                    if (valid) {
                        // 弹出遮罩层
                        this.loading = true
                        // 保存数据
                        saveOaAccountManagement(this.form).then(response => {
                            if (response.code === 200) {
                                // 点击的按钮是保存并启动流程
                                this.form.id = response.data.id
                                if (type === 'process') {
                                    // 启动流程
                                    this.startProcess(response.data.id)
                                // 点击的仅保存
                                } else {
                                    this.msgSuccess(this.$t('保存成功'))
                                    // 关闭form页面
                                    this.closeForm()
                                }
                                resolve()
                            } else {
                                this.msgError(response.msg)
                                this.loading = false
                                reject()
                            }
                        })
                    } else {
                      this.locationError(document)
                    }
                })
              })
           },
            // 启动流程
           startProcess: function(dataId) {
               this.loading = false
               // 启动流程时候传入的参数
               const processData = {}
               // 待办标题
               processData.title = 'OA账号管理：' + this.form.actTitle
               // 业务表名
               processData.dataTable = 'oa_account_management'
               // 业务ID
               processData.dataId = dataId
               // 业务表单地址(待办页面需要打开的页面)
               processData.formPath = '@/views/oa/daily/accountManagement/oaAccountManagementForm'
               processData.viewPath = '@/views/oa/daily/accountManagement/oaAccountManagementView'
               // 菜单标识(根据菜单标识判断的走哪个流程)
               processData.menuKey = this.menuKey
             this.startProcessByMenu(processData, () => {
                   this.form.actStatus = 'D'
                   this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
                   this.closeForm()
               })
           }

        }

    }
</script>
